﻿using demo86.Models;
using HtmlAgilityPack;
using PagedList;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace demo86.Controllers
{

    public class ResultController : Controller
    {

        // GET: Result
        ResultDbContext db = new ResultDbContext();

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(List<Result> getList, string Search, string currentFilter)
        {
            List<Result> list = new List<Result>();
            List<Result> list2 = new List<Result>();
            HtmlWeb htmlWeb = new HtmlWeb();

            //HtmlDocument htmlDoc1 = htmlWeb.Load(Search);

            //ViewBag.ss = htmlDoc1.DocumentNode.InnerHtml;

            #region http://www.cnblogs.com


            if (Search == "http://www.cnblogs.com")

            {
                ViewBag.s1 = "2";
                HtmlDocument htmlDoc = htmlWeb.Load(Search);

                ////获取源代码 
                //ViewBag.ss = htmlDoc.DocumentNode.InnerHtml;
                //选择博客园首页文章列表

                htmlDoc.DocumentNode.SelectNodes("//div[@id='post_list']/div[@class='post_item']").
                    //"//div[@class='ProjectList']/table[@width='100%']"
                    AsParallel().ToList().ForEach(ac =>
                   {
                       //    //抓取图片，因为有空的，所以拿变量存起来
                       HtmlNode node = ac.SelectSingleNode(".//p[@class='post_item_summary']/a/img");
                       //    //".//p[@class='detail']/a/img"

                       list.Add(new Result
                       {
                           url = ac.SelectSingleNode(".//a[@class='titlelnk']").Attributes["href"].Value,

                           // url = ac.SelectSingleNode(".//a[@class='more']").Attributes["href"].Value,
                           title = ac.SelectSingleNode(".//a[@class='titlelnk']").InnerText,
                           //图片如果为空，显示默认图片
                           //  img = node == null ? VirtualPathUtility.ToAbsolute("~/Content/img/avatar.png") : node.Attributes["src"].Value,
                           img = node == null ? VirtualPathUtility.ToAbsolute("/images/avatat.png") : node.Attributes["src"].Value,
                           content = ac.SelectSingleNode(".//p[@class='post_item_summary']").InnerText
                           //     content = ac.SelectSingleNode(".//p[@class='detail']").InnerText
                       });
                   });

                //SaveToDatabase(list);

                #region 作废
                //foreach (var temp in list)
                //{
                //    Result results = new Result
                //    {
                //        url = temp.url,
                //        title = temp.title,
                //        img = temp.img,
                //        content = temp.content
                //    };
                //    var pd = db.Results.Where(u => u.title == temp.title);
                //    if (pd.Count() > 0)
                //    {
                //        //若该条目已存在
                //    }
                //    else
                //    {
                //        //该条目不存在 保存数据
                //        db.Results.AddOrUpdate(results);
                //        db.SaveChanges();
                //    }
                //}
                #endregion

                return View(list);
            }
            #endregion
            #region http://www.oschina.net/project/lang/194/csharp


            if (Search == "http://www.oschina.net/project/lang/194/csharp")

            {
                ViewBag.s1 = "2";
                HtmlDocument htmlDoc = htmlWeb.Load(Search);

                ////获取源代码 
                //ViewBag.ss = htmlDoc.DocumentNode.InnerHtml;
                //选择博客园首页文章列表

                htmlDoc.DocumentNode.SelectNodes("//ul[@class='List']/li[@class='recomm']").
                     //"//div[@class='ProjectList']/table[@width='100%']
                     AsParallel().ToList().ForEach(al =>
                     {
                         HtmlNode node = al.SelectSingleNode(".//p[@class='detail']/a/img");
                         list.Add(new Result
                         {
                             url = "http://www.oschina.net" + al.SelectSingleNode(".//a[@target='_blank']").Attributes["href"].Value,
                             title = al.SelectSingleNode(".//a[@target='_blank']").InnerText,
                             img = node == null ? VirtualPathUtility.ToAbsolute("/images/avatat.png") : node.Attributes["src"].Value,
                             content = al.SelectSingleNode(".//p[@class='detail']").InnerText
                         });
                     });

                SaveToDatabase(list);

                #region 作废
                //foreach (var temp in list)
                //{
                //    Result results = new Result
                //    {
                //        url = temp.url,
                //        title = temp.title,
                //        img = temp.img,
                //        content = temp.content
                //    };
                //    var pd = db.Results.Where(u => u.title == temp.title);
                //    if (pd.Count() > 0)
                //    {

                //    }
                //    else
                //    {
                //        db.Results.AddOrUpdate(results);
                //        db.SaveChanges();
                //    }
                //}
                #endregion

                return View(list);
            }
            #endregion
            #region http://www.btime.com


            if (Search == "http://www.btime.com")

            {
                ViewBag.s1 = "2";
                HtmlDocument htmlDoc = htmlWeb.Load(Search);

                htmlDoc.DocumentNode.SelectNodes("//div[@class='article-list']/div[@class='N-mod N-mod-top']").
                     //  htmlDoc.DocumentNode.SelectNodes("//div[@class='article-list']/div[@class='article-txt']").
                     // htmlDoc.DocumentNode.SelectNodes(".//div[@class='zu-top-feed-list']/div[@class='feed-item feed-item-hook  folding']").

                     AsParallel().ToList().ForEach(al =>
                     {
                         HtmlNode node = al.SelectSingleNode(".//div[@class='pic']/a/img");
                         list.Add(new Result
                         {
                             url = al.SelectSingleNode(".//div[@class='pic']/a").Attributes["href"].Value,
                             title = al.SelectSingleNode(".//div[@class='txt']/*/a").InnerText,
                             img = node == null ? VirtualPathUtility.ToAbsolute("/images/avatat.png") : node.Attributes["src"].Value,
                             content = al.SelectSingleNode(".//div[@class='txt']/ul[@class='txt-list']").InnerText

                             //ul[@class='txt-list']
                         });
                     });

                SaveToDatabase(list);

                #region 作废
                //foreach (var temp in list)
                //{
                //    Result results = new Result
                //    {
                //        url = temp.url,
                //        title = temp.title,
                //        img = temp.img,
                //        content = temp.content
                //    };
                //    var pd = db.Results.Where(u => u.title == temp.title);
                //    if (pd.Count() > 0)
                //    {

                //    }
                //    else
                //    {
                //        db.Results.AddOrUpdate(results);
                //        db.SaveChanges();
                //    }
                //}
                #endregion

                return View(list);
            }
            #endregion
            #region 师大

            if (Search != null && (Search.Contains("师大") || Search.Contains("师范") || Search.Contains("www.fjnu.edu")))

            {
                ViewBag.s1 = "2";
                HtmlDocument htmlDoc = htmlWeb.Load("https://www.fjnu.edu.cn/13/list.htm");
                htmlDoc.DocumentNode.SelectNodes("//div[@id='wp_news_w4']/table/*/tr[@class='firstRow']").
                     AsParallel().ToList().ForEach(al =>
                 {
                     string node = al.SelectSingleNode(".//td[@align='left']/a").Attributes["href"].Value;
                     list.Add(new Result
                     {
                         url = node.Contains("http") == false ? "https://www.fjnu.edu.cn" + node : node,
                         title = al.SelectSingleNode(".//td[@align='left']/a").InnerText,
                         date = DateTime.Parse(al.SelectSingleNode(".//div[@style='white-space:nowrap']").InnerText),
                         Where = "福建师范大学",
                         Type = "师大要闻"
                     });
                 });

                //SaveToDatabase(list);

                #region 作废
                //foreach (var temp in list)
                //{
                //    Result results = new Result
                //    {
                //        url = temp.url,
                //        title = temp.title,
                //        img = temp.img,
                //        content = temp.content,
                //        date = temp.date,
                //        Where = temp.Where,
                //        author = temp.author,
                //        Type = temp.Type
                //    };
                //    var pd = db.Results.Where(u => u.title == temp.title);
                //    if (pd.Count() > 0)
                //    {

                //    }
                //    else
                //    {
                //        db.Results.AddOrUpdate(results);
                //        db.SaveChanges();
                //    }
                //}
                #endregion

                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 江夏学院

            if (Search != null && (Search.Contains("江夏") || Search.Contains("夏院") || Search.Contains("fjjxu.edu")))

            {
                ViewBag.s1 = "2";
                HtmlDocument htmlDoc = htmlWeb.Load("http://www.fjjxu.edu.cn/html/xwzx/xxxw/1.html");
                htmlDoc.DocumentNode.SelectNodes("//div[@class='list_main_content']/ul/li").
                     AsParallel().ToList().ForEach(al =>
                     {
                         string node = al.SelectSingleNode(".//span[@class='ecms_list_title']/a").Attributes["href"].Value;
                         list.Add(new Result
                         {
                             url = node.Contains("http") == false ? "http://www.fjjxu.edu.cn" + node : node,
                             title = al.SelectSingleNode(".//span[@class='ecms_list_title']/a").InnerText,
                             date = DateTime.Parse(al.SelectSingleNode(".//span[@class='ecms_list_time']").InnerText),
                             Where = "福建江夏学院",
                             Type = "夏院要闻"
                         });
                     });
                HtmlDocument htmlDoc1 = htmlWeb.Load("http://www.fjjxu.edu.cn/html/tzgg/1.html");
                htmlDoc1.DocumentNode.SelectNodes("//div[@class='list_main_content']/ul/li").
                     AsParallel().ToList().ForEach(al =>
                     {
                         string node = al.SelectSingleNode(".//span[@class='list_title']/a").Attributes["href"].Value;
                         list.Add(new Result
                         {
                             url = node.Contains("http") == false ? "http://www.fjjxu.edu.cn" + node : node,
                             title = al.SelectSingleNode(".//span[@class='list_title']/a").InnerText,
                             date = DateTime.Parse(al.SelectSingleNode(".//span[@class='list_time']").InnerText),
                             Where = "福建江夏学院",
                             Type = "通知公告"
                         });
                     });

                //foreach (var temp in list)
                //{
                //    Result results = new Result
                //    {
                //        url = temp.url,
                //        title = temp.title,
                //        img = temp.img,
                //        content = temp.content,
                //        date = temp.date,
                //        Where = temp.Where,
                //        author = temp.author,
                //        Type = temp.Type

                //    };
                //    var pd = db.Results.Where(u => u.title == temp.title);
                //    if (pd.Count() > 0)
                //    {

                //    }
                //    else
                //    {
                //        db.Results.AddOrUpdate(results);
                //        db.SaveChanges();
                //    }
                //}
                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 志诚学院

            if (Search != null && (Search.Contains("志诚") || Search.Contains("志诚学院") || Search.Contains("fdzcxy")))

            {
                ViewBag.s1 = "2";
                //string Search5 = "http://www.fdzcxy.com/list.asp?BCid=2&SCid=7";

                //var encode5 = GetEncode(Search5);
                //htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode5);
                //HtmlDocument htmlDoc = htmlWeb.Load(Search5);
                //string html = htmlDoc.DocumentNode.InnerHtml;
                //if (!isLuan(html))
                //{
                //    encode5 = "utf-8";
                //}
                //else
                //{
                //    encode5 = "gb2312";
                //}
                //htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode5);
                //htmlDoc = htmlWeb.Load(Search5);



                string Search1 = "http://www.fdzcxy.com/list.asp?BCid=2&SCid=7";
                var encode2 = GetEncode(Search1);
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode2);
                HtmlDocument htmlDoc = htmlWeb.Load(Search1);
                //HtmlDocument htmlDoc = htmlWeb.Load("http://www.fdzcxy.com/list.asp?BCid=2&SCid=7");
                htmlDoc.DocumentNode.SelectNodes("//div[@class='mainlist']/ul/li").
                     AsParallel().ToList().ForEach(al =>
                     {
                         string node = al.SelectSingleNode(".//a").Attributes["href"].Value;
                         list.Add(new Result
                         {
                             url = node.Contains("http") == false ? "http://www.fdzcxy.com/" + node : node,
                             title = al.SelectSingleNode(".//a").InnerText,
                             date = DateTime.Parse(al.SelectSingleNode(".//span").InnerText.Replace("(", "").Replace(")", "")),
                             Where = "福建志诚学院",
                             Type = "志诚要闻"
                         });
                     });


                //foreach (var temp in list)
                //{
                //    Result results = new Result
                //    {
                //        url = temp.url,
                //        title = temp.title,
                //        img = temp.img,
                //        content = temp.content,
                //        date = temp.date,
                //        Where = temp.Where,
                //        author = temp.author,
                //        Type = temp.Type

                //    };
                //    var pd = db.Results.Where(u => u.title == temp.title);
                //    if (pd.Count() > 0)
                //    {

                //    }
                //    else
                //    {
                //        db.Results.AddOrUpdate(results);
                //        db.SaveChanges();
                //    }
                //}
                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 警察学院   要闻

            if (Search != null && (Search.Contains("警察学院") || Search.Contains("警院") || Search.Contains("fjpsc.edu")))
            //https://www.fjnu.edu.cn/d9985623-6108-4811-9a80-aaa6d119d56f.html
            //http://www.fjpsc.edu.cn/d9985623-6108-4811-9a80-aaa6d119d56f.html
            //https://www.fjnu.edu.cn/d9985623-6108-4811-9a80-aaa6d119d56f.html
            //http://www.fjnu.edu.cn/d9985623-6108-4811-9a80-aaa6d119d56f.html
            //http://www.fjpsc.edu.cn/d9985623-6108-4811-9a80-aaa6d119d56f.html
            {
                ViewBag.s1 = "2";
                HtmlDocument htmlDoc = htmlWeb.Load("http://www.fjpsc.edu.cn/3dc229a0-9481-42ed-b6ff-32da44390138_0.html");
                htmlDoc.DocumentNode.SelectNodes("//table[@class='table1']").
                     AsParallel().ToList().ForEach(al =>
                     {
                         string node = al.SelectSingleNode(".//td/a").Attributes["href"].Value;
                         list.Add(new Result
                         {
                             url = node.Contains("http") == false ? "http://www.fjpsc.edu.cn/" + node : node,
                             title = al.SelectSingleNode(".//td/a").InnerText,
                             date = DateTime.Parse(al.SelectSingleNode(".//td[2]").InnerText),
                             Where = "福建警察学院",
                             Type = "警院要闻"
                         });
                     });

                foreach (var temp in list)
                {
                    Result results = new Result
                    {
                        url = temp.url,
                        title = temp.title,
                        img = temp.img,
                        content = temp.content,
                        date = temp.date,
                        Where = temp.Where,
                        author = temp.author,
                        Type = temp.Type

                    };
                    var pd = db.Results.Where(u => u.title == temp.title);
                    if (pd.Count() > 0)
                    {

                    }
                    else
                    {
                        db.Results.AddOrUpdate(results);
                        db.SaveChanges();
                    }
                }
                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 华南女子

            if (Search != null && (Search.Contains("华南女子") || Search.Contains("华南") || Search.Contains("女子") || Search.Contains("hnwomen")))

            {
                ViewBag.s1 = "2";
                string Search5 = "http://www.hnwomen.com.cn/xin/news";
                var encode5 = GetEncode(Search5);
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode5);
                HtmlDocument htmlDoc = htmlWeb.Load(Search5);
                string html = htmlDoc.DocumentNode.InnerHtml;
                if (!isLuan(html))
                {
                    encode5 = "utf-8";
                }
                else
                {
                    encode5 = "gb2312";
                }
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode5);
                htmlDoc = htmlWeb.Load(Search5);

                htmlDoc.DocumentNode.SelectNodes("//ul[@class='news']/li").
                     AsParallel().ToList().ForEach(al =>
                     {
                         string node = al.SelectSingleNode(".//a[@id='n1']").Attributes["href"].Value;
                         list.Add(new Result
                         {
                             url = node.Contains("http") == false ? "http://www.hnwomen.com.cn" + node : node,
                             title = al.SelectSingleNode(".//a[@id='n1']").InnerText,
                             date = DateTime.Parse(al.SelectSingleNode(".//a[@id='n3']").InnerText),
                             Where = "福建华南女子学院",
                             Type = "华南要闻"
                         });
                     });
                string Search1 = "http://www.hnwomen.com.cn/xin/gg";
                var encode = GetEncode(Search5);
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode);
                HtmlDocument htmlDoc1 = htmlWeb.Load(Search1);
                string html1 = htmlDoc1.DocumentNode.InnerHtml;
                if (!isLuan(html))
                {
                    encode5 = "utf-8";
                }
                else
                {
                    encode5 = "gb2312";
                }
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode5);
                htmlDoc = htmlWeb.Load(Search5);

                htmlDoc.DocumentNode.SelectNodes("//ul[@class='news']/li").
                     AsParallel().ToList().ForEach(al =>
                     {
                         string node = al.SelectSingleNode(".//a[@id='n1']").Attributes["href"].Value;
                         list.Add(new Result
                         {
                             url = node.Contains("http") == false ? "http://www.hnwomen.com.cn" + node : node,
                             title = al.SelectSingleNode(".//a[@id='n1']").InnerText,
                             date = DateTime.Parse(al.SelectSingleNode(".//a[@id='n3']").InnerText),
                             Where = "福建华南女子学院",
                             Type = "华南公告"
                         });
                     });

                //foreach (var temp in list)
                //{
                //    Result results = new Result
                //    {
                //        url = temp.url,
                //        title = temp.title,
                //        img = temp.img,
                //        content = temp.content,
                //        date = temp.date,
                //        Where = temp.Where,
                //        author = temp.author,
                //        Type = temp.Type

                //    };
                //    var pd = db.Results.Where(u => u.title == temp.title);
                //    if (pd.Count() > 0)
                //    {

                //    }
                //    else
                //    {
                //        db.Results.AddOrUpdate(results);
                //        db.SaveChanges();
                //    }
                //}
                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 闽江

            if (Search != null && (Search.Contains("闽院") || Search.Contains("闽江") || Search.Contains("www.mju.edu")))

            {
                ViewBag.s1 = "2";
                HtmlDocument htmlDoc = htmlWeb.Load("http://www.mju.edu.cn/html/xxyw/1.html");
                htmlDoc.DocumentNode.SelectNodes("//div[@class='list_main_content']/ul/li").
                     AsParallel().ToList().ForEach(al =>
                     {
                         string node = al.SelectSingleNode(".//span[@class='list_title']/a").Attributes["href"].Value;
                         list.Add(new Result
                         {
                             url = node.Contains("http") == false ? "http://www.mju.edu.cn" + node : node,
                             title = al.SelectSingleNode(".//span[@class='list_title']/a").InnerText,
                             date = DateTime.Parse(al.SelectSingleNode(".//span[@class='list_time']").InnerText),
                             Where = "福建闽江",
                             Type = "闽江要闻"
                         });
                     });

                foreach (var temp in list)
                {
                    Result results = new Result
                    {
                        url = temp.url,
                        title = temp.title,
                        img = temp.img,
                        content = temp.content,
                        date = temp.date,
                        Where = temp.Where,
                        author = temp.author,
                        Type = temp.Type

                    };
                    var pd = db.Results.Where(u => u.title == temp.title);
                    if (pd.Count() > 0)
                    {

                    }
                    else
                    {
                        db.Results.AddOrUpdate(results);
                        db.SaveChanges();
                    }
                }
                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 农大


            if (Search != null && (Search == "https://www.fafu.edu.cn" || Search.Contains("农大") || Search.Contains("www.fafu.edu") || Search.Contains("农林")))

            {
                //List<int> listnd = new List<int>() {76,68,73,57,58,59,61,80,84 };
                ViewBag.s1 = "2";

                HtmlDocument htmlDoc = htmlWeb.Load("http://www.fafu.edu.cn/5280/list.htm");
                htmlDoc.DocumentNode.SelectNodes("//div[@id='wp_news_w3']/table/*/tr").
                     AsParallel().ToList().ForEach(al =>
                     {
                         string node = al.SelectSingleNode(".//a").Attributes["href"].Value;
                         list.Add(new Result
                         {
                             url = node.Contains("http") == false ? "http://www.fafu.edu.cn" + node : node,
                             title = al.SelectSingleNode(".//td[@align='left']/a").InnerText,
                             date = DateTime.Parse(al.SelectSingleNode(".//div[@style='white-space:nowrap']").InnerText),
                             Where = "福建农林大学",
                             Type = "农大公告"
                         });
                     });


                ViewBag.total = list.Count();
                return View(list);
                //foreach (var temp in list)
                //{
                //    Result results = new Result
                //    {
                //        url = temp.url,
                //        title = temp.title,
                //        img = temp.img,
                //        content = temp.content,
                //        date = temp.date,
                //        Where = temp.Where,
                //        author = temp.author,
                //        Type = temp.Type

                //    };
                //    var pd = db.Results.Where(u => u.title == temp.title);
                //    if (pd.Count() > 0)
                //    {

                //    }
                //    else
                //    {
                //        db.Results.AddOrUpdate(results);
                //        db.SaveChanges();
                //    }
                //}

            }
            #endregion
            #region 福大


            if (Search != null && (Search.Contains("福大") || Search.Contains("fzu") || Search.Contains("福州大学")))
            //Search!=null&&(Search.Contains("福大")||Search.Contains("fzu")||Search.Contains("福州大学"))
            {
                Search = "http://www.fzu.edu.cn";
                ViewBag.s1 = "2";

                HtmlDocument htmlDoc1 = htmlWeb.Load("http://news.fzu.edu.cn/html/fdyw/1.html");
                htmlDoc1.DocumentNode.SelectNodes("//div[@class='list_main_content']/*/li").

                     AsParallel().ToList().ForEach(al =>
                     {
                         string url1 = al.SelectSingleNode(".//a").Attributes["href"].Value;
                         //string= node == null ? VirtualPathUtility.ToAbsolute("/images/avatat.png") : node.Attributes["src"].Value,
                         list.Add(new Result
                         {
                             url = url1.Contains("http") == false ? "http://www.fzu.edu.cn" + url1 : url1,
                             title = al.SelectSingleNode(".//a").InnerText,
                             Where = "福州大学",
                             Type = "福大要闻",
                             date = DateTime.Parse(al.SelectSingleNode(".//span").InnerText)


                         });
                     });
                HtmlDocument htmlDoc = htmlWeb.Load(Search);
                htmlDoc.DocumentNode.SelectNodes("//dl[@class='newslist']/dd").

                     AsParallel().ToList().ForEach(al =>
                     {
                         string url1 = al.SelectSingleNode(".//a").Attributes["href"].Value;
                         //string= node == null ? VirtualPathUtility.ToAbsolute("/images/avatat.png") : node.Attributes["src"].Value,
                         list.Add(new Result
                         {
                             url = url1.Contains("http") == false ? "http://www.fzu.edu.cn" + url1 : url1,
                             title = al.SelectSingleNode(".//a").InnerText,
                             Type = "首页",
                             Where = "福州大学",
                             date = DateTime.Now

                         });
                     });
                foreach (var temp in list)
                {
                    Result results = new Result
                    {


                        url = temp.url,
                        title = temp.title,
                        img = temp.img,
                        content = temp.content,
                        Type = temp.Type,
                        Where = temp.Where,
                        date = temp.date,
                        author = temp.author
                    };
                    var pd = db.Results.Where(u => u.title == temp.title);
                    if (pd.Count() > 0)
                    {

                    }
                    else
                    {
                        db.Results.AddOrUpdate(results);
                        db.SaveChanges();

                    }
                }


                ViewBag.total = list.Count();

                return View(list);
            }
            #endregion
            #region   协和学院  
            if (Search != null && (Search.Contains("cuc.fjnu.edu") || Search.Contains("协和")))
            {

                ViewBag.s1 = "2";
                Search = "http://cuc.fjnu.edu.cn";
                string encode = Search + "/channels/4351.html";


                HtmlDocument htmlDoc = htmlWeb.Load(encode);
                htmlDoc.DocumentNode.SelectNodes("//div[@class='list-normal']/h2").
                         AsParallel().ToList().ForEach(al =>
                         {
                             list.Add(new Result
                             {
                                 //    img = "http://cuc.fjnu.edu.cn" + al.SelectSingleNode(".//img").Attributes["src"].Value,
                                 url = al.SelectSingleNode(".//a").Attributes["href"].Value,
                                 title = al.SelectSingleNode(".//a").InnerText,
                                 Where = "协和学院",
                                 Type = "头条新闻",
                                 date = DateTime.Parse(al.SelectSingleNode(".//span").InnerText)
                             });
                         });
                string encode1 = Search + "/channels/4352.html";
                HtmlDocument htmlDoc1 = htmlWeb.Load(encode1);
                htmlDoc1.DocumentNode.SelectNodes("//div[@class='list-normal']/h2").
                         AsParallel().ToList().ForEach(al =>
                         {
                             list.Add(new Result
                             {
                                 //    img = "http://cuc.fjnu.edu.cn" + al.SelectSingleNode(".//img").Attributes["src"].Value,
                                 url = al.SelectSingleNode(".//a").Attributes["href"].Value,
                                 title = al.SelectSingleNode(".//a").InnerText,
                                 Where = "协和学院",
                                 Type = "专题",
                                 date = DateTime.Parse(al.SelectSingleNode(".//span").InnerText)
                             });
                         });

                string center = "http://cuc.fjnu.edu.cn/newscenter";
                string centerencode = GetEncode(center);
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(centerencode);
                HtmlDocument centerhtml = htmlWeb.Load(center);
                centerhtml.DocumentNode.SelectNodes("//div[@class='menu']").AsParallel().ToList().ForEach(al =>

                {
                    list2.Add(new Result
                    {
                        content = "http://cuc.fjnu.edu.cn" + al.SelectSingleNode(".//a").Attributes["href"].Value,
                        Type = al.SelectSingleNode(".//a").InnerText
                    });
                });
                ViewBag.ttt = list2.Count();


                ViewBag.t = list2[1].content;
                string Search1 = list2[1].content;

                var encode2 = GetEncode(Search1);
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode2);
                HtmlDocument htmlDoc2 = htmlWeb.Load(Search1);
                htmlDoc2.DocumentNode.SelectNodes("//table[@id='nav2_7Tabcontent_10']").
                         AsParallel().ToList().ForEach(al =>
                         {
                             list.Add(new Result
                             {

                                 url = al.SelectSingleNode(".//td[@width='563']/a").Attributes["href"].Value,
                                 title = al.SelectSingleNode(".//td[@width='563']/a").InnerText,
                                 Where = "协和学院",
                                 Type = list2[1].Type,
                                 date = DateTime.Parse(al.SelectSingleNode(".//td[@width='101']").InnerText.Replace("[", "").Replace("]", ""))
                             });
                         });
                ViewBag.t = list2[2].content;
                string Search2 = list2[2].content;

                var encode3 = GetEncode(Search2);
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode3);
                HtmlDocument htmlDoc3 = htmlWeb.Load(Search2);
                htmlDoc3.DocumentNode.SelectNodes("//table[@id='nav2_7Tabcontent_10']").
                         AsParallel().ToList().ForEach(al =>
                         {
                             list.Add(new Result
                             {

                                 url = al.SelectSingleNode(".//td[@width='563']/a").Attributes["href"].Value,
                                 title = al.SelectSingleNode(".//td[@width='563']/a").InnerText,
                                 Where = "协和学院",
                                 Type = list2[2].Type,
                                 date = DateTime.Parse(al.SelectSingleNode(".//td[@width='101']").InnerText.Replace("[", "").Replace("]", ""))
                             });
                         });
                string Search5 = list2[4].content;

                var encode5 = GetEncode(Search5);
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode5);
                HtmlDocument htmlDoc5 = htmlWeb.Load(Search5);
                htmlDoc5.DocumentNode.SelectNodes("//table[@id='nav2_7Tabcontent_10']").
                         AsParallel().ToList().ForEach(al =>
                         {
                             list.Add(new Result
                             {

                                 url = al.SelectSingleNode(".//td[@width='563']/a").Attributes["href"].Value,
                                 title = al.SelectSingleNode(".//td[@width='563']/a").InnerText,
                                 Where = "协和学院",
                                 Type = list2[4].Type,
                                 date = DateTime.Parse(al.SelectSingleNode(".//td[@width='101']").InnerText.Replace("[", "").Replace("]", ""))
                             });
                         });

                string Search4 = list2[3].content;

                var encode4 = GetEncode(Search4);
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode4);
                HtmlDocument htmlDoc4 = htmlWeb.Load(Search4);
                htmlDoc4.DocumentNode.SelectNodes("//table[@id='nav2_7Tabcontent_10']").
                         AsParallel().ToList().ForEach(al =>
                         {
                             list.Add(new Result
                             {
                                 url = al.SelectSingleNode(".//td[@width='563']/a").Attributes["href"].Value,
                                 title = al.SelectSingleNode(".//td[@width='563']/a").InnerText,
                                 Where = "协和学院",
                                 Type = list2[3].Type,
                                 date = DateTime.Parse(al.SelectSingleNode(".//td[@width='101']").InnerText.Replace("[", "").Replace("]", ""))
                             });
                         });

                #region 数据库

                foreach (var temp in list)
                {
                    Result results = new Result
                    {
                        url = temp.url,
                        title = temp.title,
                        img = temp.img,
                        content = temp.content,
                        Type = temp.Type,
                        Where = temp.Where,
                        date = temp.date,
                        author = temp.author
                    };
                    var pd = db.Results.Where(u => u.title == temp.title);
                    if (pd.Count() > 0)
                    { }
                    else
                    {
                        db.Results.AddOrUpdate(results);
                        db.SaveChanges();
                    }
                }
                #endregion
                ViewBag.total = list.Count();
                return View(list);

            }
            #endregion
            #region 工程学院 失败


            if (Search != null && (Search.Contains("工程") || Search.Contains("工院") || Search.Contains("fjut.edu")))
            //Search!=null&&(Search.Contains("福大")||Search.Contains("fzu")||Search.Contains("福州大学"))

            {
                ViewBag.s1 = "2";

                ////   ViewBag.t = list2[1].content;
                //   string Search1 = "http://www.fjut.edu.cn/562/list.htm";

                //   var encode2 = GetEncode(Search1);
                //   htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode2);
                //   HtmlDocument htmlDoc = htmlWeb.Load(Search1);
                //string Seache = "http://www.fjut.edu.cn/562/list.htm";
                HtmlDocument htmlDoc = htmlWeb.Load("http//:www.fjut.edu.cn");
                ViewBag.ss = htmlDoc.DocumentNode.InnerHtml;
                //htmlDoc.DocumentNode.SelectNodes("//div[@id='wp_news_w2']/table/*/tr[@class='firstRow']").
                //         AsParallel().ToList().ForEach(al =>
                //         {
                //             string node = al.SelectSingleNode(".//td[@align='left']/a").Attributes["href"].Value;
                //             list.Add(new Result
                //             {
                //                 url = node.Contains("http") == false ? "http://www.fjut.edu.cn" + node : node,
                //                 title = al.SelectSingleNode(".//td[@align='left']/a").InnerText,
                //                 date = DateTime.Parse(al.SelectSingleNode(".//div[@style='white-space:nowrap']").InnerText),
                //                 Where = "福建工程学院",
                //                 Type = "工院要闻"
                //             });
                //         });

                //foreach (var temp in list)
                //{
                //    Result results = new Result
                //    {
                //        url = temp.url,
                //        title = temp.title,
                //        img = temp.img,
                //        content = temp.content,
                //        date = temp.date,
                //        Where = temp.Where,
                //        author = temp.author,
                //        Type = temp.Type

                //    };
                //    var pd = db.Results.Where(u => u.title == temp.title);
                //    if (pd.Count() > 0)
                //    {

                //    }
                //    else
                //    {
                //        db.Results.AddOrUpdate(results);
                //        db.SaveChanges();
                //    }
                //}
                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion

            #region 测试 思杰 163
            if (Search == "http://www.163.com")

            {
                ViewBag.s1 = "2";
                // string encode = "utf-8";
                //获取编码
                string encode = GetEncode(Search);
                htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode);
                HtmlDocument htmlDoc = htmlWeb.Load(Search);
                // HtmlDocument htmlDoc = htmlWeb.Load(Search);
                //   ViewBag.sj = htmlDoc;
                //  htmlDoc.DocumentNode.SelectNodes("//div[@id='sdyw']/div/tbody/tr").
                htmlDoc.DocumentNode.SelectNodes("//ul[@class='m-list list-main interval']/li[@class='hx']").
                         //     htmlDoc.DocumentNode.SelectNodes("//tr/td[@align='left']/a[@taget='_blank']").

                         AsParallel().ToList().ForEach(al =>
                         {
                             // HtmlNode node = al.SelectSingleNode(".//td/img");
                             list.Add(new Result
                             {
                                 url = al.SelectSingleNode(".//a").Attributes["href"].Value,

                                 title = al.SelectSingleNode(".//a").InnerText,
                                 // img = node == null ? VirtualPathUtility.ToAbsolute("/images/avatat.png") : node.Attributes["src"].Value,
                                 //content = al.SelectSingleNode(".//div[@class='txt']/ul[@class='txt-list']").InnerText

                                 //ul[@class='txt-list']
                             });
                         });
                #region 数据库
                //foreach (var temp in list)
                //{
                //    Result results = new Result
                //    {
                //        url = temp.url,
                //        title = temp.title,
                //        img = temp.img,
                //        content = temp.content
                //    };
                //    var pd = db.Results.Where(u => u.title == temp.title);
                //    if (pd.Count() > 0)
                //    {

                //    }
                //    else
                //    {
                //        db.Results.AddOrUpdate(results);
                //        db.SaveChanges();

                //    }


                //}
                #endregion

                return View(list);
            }
            #endregion

            return View();
        }

        /// <summary>
        /// 保存到数据库
        /// </summary>
        /// <param name="list">Result对象</param>
        public void SaveToDatabase(List<Result> list)
        {
            //!!!取不出的值请直接给个初始值!!! ""或者0
            foreach (var temp in list)
            {
                Result results = new Result
                {
                    url = temp.url,
                    title = temp.title,
                    img = temp.img,
                    content = temp.content,
                    date = temp.date,
                    author = temp.author,
                    Type = temp.Type,
                    Where = temp.Type
                };
                //var pd = db.Results.Where(u => u.title == temp.title);
                if (db.Results.Where(u => u.title == temp.title).Count() > 0)
                {
                    //若该条目已存在

                }
                else
                {
                    //不存在则存入
                    db.Results.AddOrUpdate(results);
                    db.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 分页 过滤器
        /// </summary>
        /// <param name="sortOrder">???</param>
        /// <param name="Search">搜索值</param>
        /// <param name="currentFilter">???</param>
        /// <param name="page">???</param>
        /// <returns></returns>
        public ActionResult Show(string sortOrder, string Search, string currentFilter, int? page)
        {
            //判断输入值是否为空，为空则直接赋值""，列出的就是所有结果
            //在输入值不为空时不再需要列出所有值,增加运行效率
            var results = from u in db.Results
                          where (u.title.Contains(Search ?? ""))
                          orderby u.title
                          select u;
            //if (!string.IsNullOrEmpty(Search))
            //{
            //    results = results.OrderBy(u => u.title.Contains(Search));
            //}
            //results = results.OrderBy(u => u.title);

            int pageSize = 8;
            int pageNumber = (page ?? 1);
            return View(results.ToPagedList(pageNumber, pageSize));
        }


        /// <summary>
        /// 获取输入页编码
        /// </summary>
        /// <param name="url">链接</param>
        /// <returns></returns>
        public string GetEncode(string url)
        {
            Uri uri = new Uri(url);
            WebClient webClient = new WebClient();
            string temp = Encoding.GetEncoding("utf-8").GetString(webClient.DownloadData(uri));
            //匹配编码
            Match charset = Regex.Match(temp, "<meta[\\s\\S]*charset=[\\s\\S]*?([-a-zA-Z0-9]+)[\\s\\S]*?>");
            //判断是否获取到编码
            if (string.IsNullOrEmpty(charset.Groups[1].Value))
                return isLuan(temp) ? "utf-8" : "gb2312";
            else
                return charset.Groups[1].Value;
        }

        /// <summary>
        /// 判断是否存在乱码
        /// </summary>
        /// <param name="txt">页面源码</param>
        /// <returns></returns>
        bool isLuan(string txt)
        {
            var bytes = Encoding.UTF8.GetBytes(txt);
            //239 191 189
            for (var i = 0; i < bytes.Length; i++)
            {
                if (i < bytes.Length - 3)
                    if (bytes[i] == 239 && bytes[i + 1] == 191 && bytes[i + 2] == 189)
                    {
                        return true;
                    }
            }
            return false;
        }

        #region 判断输入是否为网址 思杰

        //public ActionResult Result(string url)
        //{
        //    //判断是否为网址
        //    if (url.ToLower().Contains(""))
        //    {
        //        string encode = "utf-8";
        //        //获取编码
        //        encode = GetEncode(url);
        //        ViewBag.SourceCode = PageString;
        //        //获取元素
        //        HtmlWeb htmlWeb = new HtmlWeb();
        //        htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode);
        //        HtmlDocument doc = htmlWeb.Load(url);
        //        HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//*");
        //        foreach (HtmlNode item in nodes)
        //        {
        //            if (item.InnerText.Trim() != "")
        //            {
        //                ViewBag.Elements += item.InnerText;
        //            }
        //        }
        //    }
        //    else
        //    {
        //        ViewBag.Error = true;
        //        ViewBag.E = "Pleas Input A Correct Adress With 'Http'";
        //    }
        //    return View("Index");
        //}

        #endregion

    }
}